<template>
  <div class="com-container">
    <div class="com-chart" ref="stock_ref"></div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      chartInstance: null,
      allData: null,
      currentIndex: 0,
      timeId: null,
      //   屏幕大小改变时，设置titleFontSize
      titleFontSize: 0
    }
  },
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter)
    clearInterval(this.timeId)
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.stock_ref, 'chalk')
      //   五个圆环对应的圆心点
      const centerPoints = [
        ['18%', '40%'],
        ['50%', '40%'],
        ['82%', '40%'],
        ['34%', '75%'],
        ['66%', '75%']
      ]
      const initOption = {
        title: {
          text: '▎ 库存销售量',
          left: 20,
          top: 20
        },
        series: [
          {
            type: 'pie',
            center: centerPoints[0],
            hoverAnimation: false,
            label: {
              show: true,
              position: 'center'
            },
            labelLine: {
              show: false
            }
          },
          {
            type: 'pie',
            center: centerPoints[1],
            hoverAnimation: false,
            label: {
              show: true,
              position: 'center'
            },
            labelLine: {
              show: false
            }
          },
          {
            type: 'pie',
            center: centerPoints[2],
            hoverAnimation: false,
            label: {
              show: true,
              position: 'center'
            },
            labelLine: {
              show: false
            }
          },
          {
            type: 'pie',
            center: centerPoints[3],
            hoverAnimation: false,
            label: {
              show: true,
              position: 'center'
            },
            labelLine: {
              show: false
            }
          },
          {
            type: 'pie',
            center: centerPoints[4],
            hoverAnimation: false,
            label: {
              show: true,
              position: 'center'
            },
            labelLine: {
              show: false
            }
          }
        ]
      }
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('mouseover', () => {
        clearInterval(this.timeId)
      })
      this.chartInstance.on('mouseout', () => {
        this.startInterval()
      })
    },
    async getData() {
      const { data: res } = await this.$http.get('stock')
      this.allData = res
      console.log(this.allData)
      this.updateChart()
      this.startInterval()
    },
    updateChart() {
      // 增加5个圆环的颜色变化范围
      const colorArrs = [
        ['#4FF778', '#0BA82C'],
        ['#E5DD45', '#E8B11C'],
        ['#E8821C', '#E55445'],
        ['#5052EE', '#AB6EE5'],
        ['#23E5E5', '#2E72BF']
      ]
      const start = this.currentIndex * 5
      const end = (this.currentIndex + 1) * 5
      //   先显示5条数据
      const showData = this.allData.slice(start, end)
      const seriesArr = showData.map((item, index) => {
        return {
          data: [
            {
              name: item.name + '\n\n' + item.sales,
              value: item.sales,
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  {
                    offset: 0,
                    color: colorArrs[index][0]
                  },
                  {
                    offset: 1,
                    color: colorArrs[index][1]
                  }
                ])
              }
            },
            {
              value: item.stock,
              itemStyle: {
                color: '#333843'
              }
            }
          ],
          //   鼠标移入动画移除
          hoverAnimation: false,
          //   指示线的移除
          labelLine: {
            show: false
          },
          //   label的显示位置
          label: {
            show: true,
            position: 'center',
            color: colorArrs[index][0]
          }
        }
      })
      const dataOption = {
        series: seriesArr
      }
      this.chartInstance.setOption(dataOption)
    },
    screenAdapter() {
      this.titleFontSize = (this.$refs.stock_ref.offsetWidth / 100) * 3.6
      //   重新设置内圆环半径
      const innerRadius = this.titleFontSize * 2.8
      const outterRadius = innerRadius * 1.25
      //   重新设置外圆环半径
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize
          }
        },
        series: [
          {
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: this.titleFontSize / 2
            }
          },
          {
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: this.titleFontSize / 2
            }
          },
          {
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: this.titleFontSize / 2
            }
          },
          {
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: this.titleFontSize / 2
            }
          },
          {
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: this.titleFontSize / 2
            }
          }
        ]
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    startInterval() {
      if (this.timeId) {
        clearInterval(this.timeId)
      }
      this.timeId = setInterval(() => {
        this.currentIndex++
        if (this.currentIndex > 1) {
          this.currentIndex = 0
        }
        this.updateChart()
      }, 3000)
    }
  }
}
</script>
<style lang="less" scoped>
</style>
